# -*- coding: utf-8 -*- 
# @Time : 2020/12/23 9:45 上午 
# @Author : Forest
# @File : docker_install.py
#!/usr/bin/python3


import os
import subprocess
import argparse
import time


# 定义shell
def base(cmd):
    if subprocess.call(cmd, shell=True):
        raise Exception("{} 执行失败,请检查".format(cmd))


# 定义docker函数
def i_docker():
    print("检测依赖环境")
    time.sleep(3)
    base("yum -y install yum-utils device-mapper-persistent-data lvm2 > /dev/null")
    base("yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo > /dev/null")
    base("yum -y install docker-ce > /dev/null")
    print(" Write fast json.....")
    if (not os.path.exists("/etc/docker")):
        os.mkdir('/etc/docker/')
    with open('/etc/docker/daemon.json','w') as f:
        f.write('{\n    "registry-mirrors": ["https://9f4w4icn.mirror.aliyuncs.com"] \n}')
    base('sudo systemctl daemon-reload')
    base('sudo systemctl restart docker.service')

def i_docker_com():
    print("安装docker-compose")
    base("curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose")
    base("sudo chmod +x /usr/local/bin/docker-compose")
i_docker()
i_docker_com()